import sys
import time
from decimal import Decimal
sys.path.append("../modules/")
from BayesNetwork import BayesNetwork

BN = BayesNetwork()

class Experiment():
    def __init__(self):
        self.ini_structure = None
        self.class_variable = None
        BN.showProgress = False
        BN.showTime = False
        BN.cores = 1
     
    def training(self, variables, data):
        BN.parse_encog("./data/EncogBayesData/cancer.eg")
        
        # Target structure
        target_structure = BN.copy(BN.BayesianNetwork)
        #BN.generateBNStructureVisualization("target",target_structure)
        
        # Creating initial structure
        initial_structure = BN.generateRandomStructure(BN.BayesianNetwork.keys(), Decimal(0.1), 4)
        self.ini_structure = initial_structure
        initial_structure = BN.randomParameterBNInitialization(initial_structure)
        BN.export_encog("log_ini_structure.eg", initial_structure)
        BN.generateBNStructureVisualization("ini_structure", initial_structure)
        
        BN.BayesianNetwork = None
        
        # DEFINE CLASS VARIABLE HERE
        class_variable = "Coma"
        self.class_variable = class_variable
        
        
        # DEFINE EXPERIMENT
        print "EXPERIMENT 1 -  SEM, Masters search, With MI = 0.5, " + class_variable
        st = time.time()
        bn_exp1 = BN.structuralExpectationMaximization(variables, data, "final_structure_exp1", Decimal(0.01), "BBSL", {'class_variable': class_variable, 'classification_threshold': Decimal(0.7), 'external_edges': True},   initial_structure, Decimal(0.5), 10)
        print "Total time for experiment: " + str(time.time() - st)
        
        return bn_exp1

        
    def test(self,variables, data, structure):
        BN.BayesianNetwork = None
        #inverted, added, removed, likelihood_bn1, likelihood_bn2, bic_bn1, bic_bn2, cond_1, cond_2 = BN.compareBNStructures(self.ini_structure, structure, variables, data, class_variable=self.class_variable) 
        result= BN.compareBNStructures(self.ini_structure, structure, variables, data, class_variable=self.class_variable)
        return result
    

